package com.inspectortime.webapp.inspector;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.inspectortime.domain.Inspection;
import com.inspectortime.domain.InspectorCompany;
import com.inspectortime.domain.RealEstateAgent;
import com.inspectortime.repository.Repository;
import com.inspectortime.webapp.util.HttpSessionUtils;

/**
 * Performs two functions - Displays an "Are you sure?" page and removes the selected realtor
 * 
 * @author bones
 */
public class DeleteReferringRealtorController extends AbstractController {
	
	private static Log log = LogFactory.getLog(DeleteReferringRealtorController.class);
	
	private Repository repository;
	private String confirmView;
	private String successView;


	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ModelAndView mv = null;
		InspectorCompany company = HttpSessionUtils.retrieveLoggedInCompany(request, repository);
		
		String agentId = request.getParameter("agentId");
		String confirmParam = request.getParameter("confirm");
		
		RealEstateAgent agent = null;
		
		//pull agent from the company object instead of directly loading it
		//to ensure that a company only remove its own agents 
		Collection<RealEstateAgent> agents = company.getReferringAgents();
		for (RealEstateAgent tempAgent : agents) {
			if (tempAgent.getId().equals(agentId)) {
				agent = tempAgent;
				break;
			}
		}
		
		if (agent == null) {
			//TODO: THROW SOMETHING BETTER THAN THIS
			throw new Exception("Agent not found");
		}
		
		
		//Send user to confirm page
		if (StringUtils.isBlank(confirmParam)) {
			mv = new ModelAndView(this.confirmView);
		} else {
			
			List<Inspection> inspections = repository.findInspectionsByReferringAgent(agent);
			if (inspections == null || inspections.size() == 0) {
				//there are no inspections that this agent is associated with
				//let's remove it from DB to reduce junk data
				company.getReferringAgents().remove(agent);
				company.save();
				repository.deleteRealEstateAgent(agent);
				
			} else {
				//just unlink from the company and mark the date
				company.getReferringAgents().remove(agent);
				company.save();
				
				agent.setDateDeleted(new Date());
				agent.save();
			}
			
			mv = new ModelAndView(this.successView);
		}

		mv.addObject("agent", agent);
		mv.addObject("company", company);
		//mv.addObject("allAgents", repository.findAllRealEstateAgents()); //TEST
		return mv;
	}
	
	public void setRepository(Repository repository) {
		this.repository = repository;
	}

	public void setConfirmView(String confirmView) {
		this.confirmView = confirmView;
	}

	public void setSuccessView(String successView) {
		this.successView = successView;
	}
	



	
}
